package wxapp

import (
    "fmt"
	"errors"
	"wxsdk/miniapp/core"
	"github.com/astaxie/beego"
)

// 修改服务器地址
type ModifyDomainRequest struct {
	Action           string `json:"action"`
    RequestDomain    []string `json:"requestdomain"`
	WsRequestDomain  []string `json:"wsrequestdomain"`
	UploadDomain     []string `json:"uploaddomain"`
	DownloadDomain   []string `json:"downloaddomain"`
}

func (r *ModifyDomainRequest) GetApiMethodName(token string) string {
	return fmt.Sprintf("https://api.weixin.qq.com/wxa/modify_domain?access_token=%s",token)
}

type ModifyDomainResponseResult struct {
	*core.ErrorResponse
	*ModifyDomainResponse
}

type ModifyDomainResponse struct {
	RequestDomain   []string `json:"requestdomain"`
	WsrequestDomain []string `json:"wsrequestdomain"`
	UploadDomain    []string `json:"uploaddomain"`
	DownloadDomain  []string `json:"downloaddomain"`
}

// 修改服务器地址
func  ModifyDomain(authorizerAccessToken,action string,requstDomain,wsRequestDomain,uploadDomain,downloadDomain []string)(string,int,error){
	client := core.GetPostJSONClient()
	var rsp ModifyDomainResponseResult
	var req = new(ModifyDomainRequest)
	req.Action = action
	req.RequestDomain = requstDomain
	req.WsRequestDomain = wsRequestDomain
	req.UploadDomain = uploadDomain
	req.DownloadDomain = downloadDomain
	serverUrl := req.GetApiMethodName(authorizerAccessToken)
	tbdata, err := client.Excute("POST", serverUrl, req, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", -5, err
	}
	if rsp.ErrorResponse.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",rsp.ErrorResponse.ErrCode, errors.New(string(tbdata)) 
	}
	return string(tbdata),0, nil
}


